/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.page.uilts;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import com.xuexiang.xui_lib.util.ColorUtils;
import com.xuexiang.xui_lib.util.DrawableUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.BasePageAbilitySlice;
import com.xuexiang.xuidemo.page.base.Page;
import com.xuexiang.xuidemo.util.DisplayUtils;

import java.io.IOException;

/**
 * DrawableUtils示例页面
 *
 * @since 2021-03-23
 */
@Page(name = "DrawableUtils", action = "DrawableUtils")
public class DrawableUtilsAbilitySlice extends BasePageAbilitySlice {
    private static final int CORNER_RADIUS = 16;
    private static final int SIZE = 20;
    private final int mRADIUS = 10;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_utils_drawable;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        int shapeSize = getDimen(ResourceTable.Float_drawable_utils_common_shape_size);
        int shapeRadius = DisplayUtils.vp2px(getContext(), mRADIUS);
        int filledColor = getColor(ResourceTable.Color_app_color_theme_3);
        Image solidImage = findCastComponentById(ResourceTable.Id_solidImage);

        // 创建一张指定大小的纯色图片，支持圆角
        Element solidElement = DrawableUtils.createDrawableWithSize(shapeSize, shapeSize, shapeRadius, filledColor);
        solidImage.setImageElement(solidElement);

        Image circleImage = findCastComponentById(ResourceTable.Id_circleImage);
        /**
         * 创建一张指定大小的圆形图片，并带文字
         */
        int theme2 = getColor(ResourceTable.Color_app_color_theme_2);
        int size = DisplayUtils.vp2px(getContext(), SIZE);
        Element circleElement = DrawableUtils.createCircleDrawableWithText(shapeSize,
            theme2, "薛", size, Color.WHITE.getValue());
        circleImage.setImageElement(circleElement);

        Image tintColorOrigin = findCastComponentById(ResourceTable.Id_tintColorOrigin);
        Image tintColorImage = findCastComponentById(ResourceTable.Id_tintColor);
        int theme1 = getColor(ResourceTable.Color_app_color_theme_1);
        Element tintColorDrawable = DrawableUtils.createDrawableWithSize(shapeSize, shapeSize, shapeRadius, theme1);
        int theme7 = getColor(ResourceTable.Color_app_color_theme_7);
        Element tintColorOriginBitmap = DrawableUtils.createDrawableWithSize(shapeSize, shapeSize, shapeRadius, theme7);

        /**
         * 其中一张重新设置颜色
         */
        tintColorOrigin.setImageElement(tintColorDrawable);
        tintColorImage.setImageElement(tintColorOriginBitmap);

        int theme6 = getColor(ResourceTable.Color_app_color_theme_6);
        Component separatorComponent = findCastComponentById(ResourceTable.Id_separator);
        DrawableUtils.createItemSeparatorBg(separatorComponent, theme7,
            theme6, getDimen(ResourceTable.Float_separator_height), true);

        int color = ColorUtils.getRandomColor();
        Button btnAction = findCastComponentById(ResourceTable.Id_btn_action);

        btnAction.setBackground(DrawableUtils.getDrawable(CORNER_RADIUS, color));
        btnAction.setTextColor(ColorUtils.isColorDark(color) ? Color.WHITE : Color.BLACK);
    }

    private int getDimen(int colorId) {
        try {
            return (int) getResourceManager().getElement(colorId).getFloat();
        } catch (IOException e) {
            return 0;
        } catch (NotExistException e) {
            return 0;
        } catch (WrongTypeException e) {
            return 0;
        }
    }
}
